package aaaMoveMana;

import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JFileChooser;
import javax.swing.JLabel;

import aaaBrowser.Reg;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

import com.attilax.io.filex;
import com.attilax.io.pathx;

public class jsHandler {
	JWebBrowser webBrowser;
	public jsHandler(JWebBrowser webBrowser) {
		 this. webBrowser=webBrowser;
	}

	public void exe(Object[] parameters) {
		String saveFolder = "C:\\";
		Preferences pref = Preferences.userRoot().node(
				this.getClass().getName());
		String lastPath = pref.get("lastPath", "");
		JFileChooser jfc = null;
		if (!lastPath.equals(""))
			jfc = new JFileChooser(lastPath);
		else
			jfc = new JFileChooser();

		jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		jfc.showDialog(new JLabel(), "选择");
		File file = jfc.getSelectedFile();
		saveFolder = jfc.getSelectedFile().getPath();
		pref.put("lastPath", saveFolder);
		if (file.isDirectory()) {
			System.out.println("文件夹:" + file.getAbsolutePath());
		} else if (file.isFile()) {
			System.out.println("文件aa2:" + file.getAbsolutePath());
			showInvokeOK(file.getAbsolutePath(),parameters);

		}
		
	}

	private void showInvokeOK(String absolutePath,Object[] parameters) {
		  String js = filex.read(pathx.classPathParent() + "/set_edit-control-val.js");
			String contrl = (String) parameters[0];
          js = js.replaceAll("@control", contrl);
          
          absolutePath=pathx.fixSlash(absolutePath);   
          absolutePath=deMapDir(absolutePath);
          js = js.replaceAll("@val", absolutePath);
          
          // "1234";
          System.out.println("exe js:" + js);
          webBrowser.executeJavascript(js);
		
	}
	
	
	private String deMapDir(String url_decode) {
		int idx2 = url_decode.indexOf("/");
		String url2 =url_decode.substring(idx2 + 1); // de webapp
		return url2;
	}

}
